package com.freshcity.page.feiji
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.feiji.FeijiHisWin;
	import com.freshcity.feiji.FeijiItemBack;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenMax;
	
	import fl.containers.ScrollPane;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;

	public class FeiJiStatus extends Component
	{
		private var feijiWriter:FeiJiWriter;
		private var data:*;
		private var feijiHis:FeijiHisWin;

		public function FeiJiStatus(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.mouseEnabled=false;
			this.tabEnabled=false;
			super(parent, xpos, ypos);
			feijiHis.newFeiJi.addEventListener(MouseEvent.CLICK,onNewFeiJi);
		}
		
		protected function onNewFeiJi(event:MouseEvent=null):void
		{
			if(main.user.permitFeiji<=0){
				main.msg.showRollTipsMsg("今日可投纸飞机名额已经用完");
				return;
			}
			this.feijiHis.visible=false;
			feijiWriter = new FeiJiWriter();
			feijiWriter.main = this.main;
			this.feijiWriter.onShow(this);
		}
		
		override public function hide(event:Event=null):void
		{
			super.hide(event);
		}
		
		override public function show(event:Event=null):void
		{
			feijiHis.visible = false;
			if(feijiWriter)feijiWriter.visible = false;
			
			main.commu.send(OpCodes.READ_MY_FEIJI, {},onGetMyFeiji);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			
			feijiHis=new FeijiHisWin();
			feijiHis.visible=false;
			addChild(feijiHis);
		}
		
		override public function set main(value:MainScene):void
		{
			super.main = value;
		}

		private function onGetMyFeiji(data:*):void
		{
			if (data == false)
			{
				this.feijiHis.visible=false;
				feijiWriter = new FeiJiWriter();
				feijiWriter.main = this.main;
				this.feijiWriter.onShow(this);
			}
			else if (data is String)
			{
				main.msg.showRollTipsMsg(data);
			}
			else
			{
				this.data=data;
				resort();
				this.feijiHis.visible=true;
			}
		}
		
		private function resort():void
		{
			var mainContent:Sprite = new Sprite();
			mainContent.mouseEnabled = false;
			var ypos:Number = 10.0;
			if (data is Array)
			{
				var s:FeijiItemBack;
				var xpos:int=10;
				
				for each (var d:* in data)
				{
					s=new FeijiItemBack();
					s.x = 10;
					s.y = ypos;
					
					s.dateLabel.text = d.createdAt;
					s.desc.text = d.content;
					s.yesAmt.text = d.yesAmt;
					s.ignoreAmt.text = d.ignoreAmt;
					
					mainContent.addChild(s);
					ypos+=95;
				}
				feijiHis.panel.source = mainContent;
			}
		}

	}
}